package com.weimob.demo.admin.utils;

import com.weimob.demo.admin.persistent.entity.TWmSysUser;
import com.weimob.demo.admin.persistent.mapper.TWmSysUserMapper;
import com.weimob.demo.admin.security.LoginUserInfo;
import com.weimob.demo.common.utils.CacheUtils;
import com.weimob.demo.common.utils.SpringUtils;
import com.weimob.demo.common.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * Created by junxiao on 2017/1/4.
 */
public class UserUtils {

    private static TWmSysUserMapper userMapper = SpringUtils.getBean(TWmSysUserMapper.class);

    public static final String USER_CACHE = "userCache";
    public static final String USER_CACHE_ID_ = "id_";
    public static final String USER_CACHE_LOGIN_NAME_ = "ln";

    public static final String CACHE_ROLE_LIST = "roleList";
    public static final String CACHE_MENU_LIST = "menuList";

    /**
     * 根据用户名取得用户
     * @param userCode
     * @return
     */
    public static TWmSysUser get(String userCode){
        TWmSysUser user = (TWmSysUser)CacheUtils.get(USER_CACHE, USER_CACHE_ID_ + userCode);
        if (user == null) {
            user = userMapper.selectByPrimaryKey(userCode);
            saveUserCache(user);
        }
        return user;
    }


    /**
     *  根据登录名获取用户
     * @param loginName
     */
    public static TWmSysUser getByLoginName(String loginName) {
        TWmSysUser user = (TWmSysUser)CacheUtils.get(USER_CACHE, USER_CACHE_LOGIN_NAME_ + loginName);
        if (user == null) {
//            user = userMapper.selectByPrimaryKey(userCode);
            saveUserCache(user);
        }
        return user;
    }

    public void clearCache() {

    }

    /**
     * 清除当前用户缓存
     * @param user
     */
    public static void clearCache(TWmSysUser user) {
        CacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getUserCode());
        CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName());
    }


    /**
     * 取得当前登录用户信息
     * @return
     */
    public static TWmSysUser getUser() {
        LoginUserInfo userInfo = getLoginUserInfo();
        if (userInfo != null) {
            TWmSysUser user = getByLoginName(userInfo.getUserName());
            return user;
        }
        return new TWmSysUser();
    }

    /**
     * 取得登录用户信息
     * @return
     */
    public static LoginUserInfo getLoginUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        LoginUserInfo userInfo = (LoginUserInfo)subject.getPrincipal();
        if (userInfo != null) {
            return userInfo;
        }
        return null;
    }


    /**
     *
     * @return
     */
    public static Session getSession() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession(false);
        if (session == null) {
            session = subject.getSession();
        }
        return session;
    }

    /**
     * 保存用户信息到缓存
     * @param user
     */
    public static void saveUserCache(TWmSysUser user) {
        if (user != null) {
            if (StringUtils.isNoneBlank(user.getUserCode())) {
                CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getUserCode());
            }
            if (StringUtils.isNotBlank(user.getLoginName())) {
                CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName());
            }
        }
    }
}
